package com.jnu.myaccount.function.sort;

import static com.jnu.myaccount.R.drawable.border;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.myaccount.DetailActivity;
import com.jnu.myaccount.R;
import com.jnu.myaccount.adapter.ItemAdapter;
import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.DataBank.DataBank;
import com.jnu.myaccount.data.GridItems;
import com.jnu.myaccount.function.home.HomeFragment;
import com.jnu.myaccount.record.PaidFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SortPaidFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SortPaidFragment extends Fragment {

    private GridView mGridView;
    private ItemAdapter mItemAdapter;
    private HomeFragment.CollectRecycleAdapter mCollectRecyclerAdapter;//自定义recyclerveiw的适配器
    private ArrayList<Data> fList=new ArrayList<>();
    //控件
    private ImageView paid_type_iv;
    private TextView paid_type_name;
    private TextView paid_type_paid;
    private RecyclerView paidRecycle;
    private LinearLayout paidType;
    private  List<GridItems> provinceBeanList = new ArrayList<>();

    public SortPaidFragment() {
        // Required empty public constructor
    }

    public static SortPaidFragment newInstance() {
        SortPaidFragment fragment = new SortPaidFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_sort_paid, container, false);
        //绑定控件
        paid_type_iv=view.findViewById(R.id.sort_type_iv);
        paid_type_name=view.findViewById(R.id.sort_type_name);
        paid_type_paid=view.findViewById(R.id.sort_type_paid);
        mGridView = view.findViewById(R.id.sort_type_items);
        paidType=view.findViewById(R.id.sort_type);
        paidRecycle=view.findViewById(R.id.sort_list);
        //gridView视图

        for (int i = 0; i < PaidFragment.itemNames.length; i++) {
            GridItems mItemsType = new GridItems(PaidFragment.itemNames[i],PaidFragment.itemImage[i]);
            provinceBeanList.add(mItemsType);
        }
        mItemAdapter = new ItemAdapter(getActivity(), provinceBeanList);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                             @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                 mItemAdapter.changeSelected(position);//点击监听
                                                 Toast.makeText(getContext(),"查看:"+PaidFragment.itemNames[position],Toast.LENGTH_SHORT).show();
                                                 paidType.setBackgroundColor(Color.parseColor("#FFDEAD"));
                                                 paid_type_name.setText("名称:"+PaidFragment.itemNames[position]);
                                                 paid_type_iv.setImageResource(PaidFragment.itemImage[position]);
                                                 paidType.setBackgroundResource(R.drawable.border);
                                                 getData(PaidFragment.itemNames[position]);
                                             }
                                         }
        );
        mGridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mItemAdapter.changeSelected(position);//选中监听
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mGridView.setAdapter(mItemAdapter);

        return view;
    }

    private void getData(String name) {
        DataBank mDataBank=new DataBank(getActivity());
        ArrayList<Data> mList=mDataBank.loadData();
        double paid=0.0;
        int count=0;
        if(fList.size()!=0) {
            fList.clear();//必须保证arraylist还是原来的arraylist
        }
        for(int j=0;j<mList.size();j++){
            Data g=mList.get(j);
            if(g.itemName.equals(name)){
                count++;
                paid+=g.itemCost;
                fList.add(g);
            }
        }
        paid_type_paid.setText("总共支出: "+paid+"元    共计"+count+"笔");
            mCollectRecyclerAdapter = new HomeFragment.CollectRecycleAdapter(getContext(),fList);
            //给RecyclerView设置adapter
            int spacingInPixels = 8;
            paidRecycle.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

            paidRecycle.setAdapter(mCollectRecyclerAdapter);
            //RecycleView 增加边距

            //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
            //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
            paidRecycle.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            //设置item的分割线
            paidRecycle.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));

            //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
            mCollectRecyclerAdapter.setOnItemClickListener(new HomeFragment.CollectRecycleAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(View view, Data data,int position) {
                    //此处进行监听事件的业务处理
                    Toast.makeText(getActivity(),data.getItemName(),Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(getActivity(), DetailActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("type","show");//表明这是一条用来展示的intent
                    bundle.putSerializable("DataInfo", data);
                    bundle.putInt("position", position);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }
    }
}